<template>
  <ul class="wrapper" v-show="isShow">
    <li class="item"
        v-for="k in citiesArr"
        :key="k"
        :ref="k"
        @click="handleLetter"
        @touchstart="handleTouchStart"
        @touchmove="handleTouchMove"
        @touchend="handleTouchEnd"
    >{{k}}</li>
  </ul>
</template>

<script>
export default {
  name: 'CityAlphabet',
  data () {
    return {
      touchStatus: false,
      startY: 0,
      timer: null
    }
  },
  props: ['cities', 'isShow'],
  computed: {
    citiesArr () {
      const temp = []
      for (let citiesKey in this.cities) {
        temp.push(citiesKey)
      }
      return temp
    }
  },
  updated () {
    this.startY = this.$refs['A'][0].offsetTop
  },
  methods: {
    handleLetter (e) {
      const letter = e.target.innerHTML
      this.$emit('change', letter)
    },
    handleTouchStart () {
      this.touchStatus = true
    },
    handleTouchMove (e) {
      if (this.touchStatus) {
        this.timer && clearTimeout(this.timer)
        this.timer = setTimeout(() => {
          const touchY = e.targetTouches[0].pageY - 79
          const letterNum = Math.floor((touchY - this.startY) / 20)
          if (letterNum >= 0 && letterNum < this.citiesArr.length) {
            this.$emit('change', this.citiesArr[letterNum])
          }
        }, 15)
      }
    },
    handleTouchEnd () {
      this.touchStatus = false
    }
  }
}
</script>

<style lang="stylus" scoped>
  @import "~_styles/varibles.styl"
  .wrapper
    display: flex
    flex-direction: column
    justify-content: center
    position: absolute
    right: 0
    bottom: 0
    top: 1.58rem
    width: .4rem
    .item
      text-align: center
      line-height: .4rem
      color: $bgColor

</style>
